#! /bin/bash
JOBNUM=4
NPWD=`realpath .`
CODETOP=`realpath ../../`
export ARCH=arm
export CROSS_COMPILE=$CODETOP/gcc/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-


if [ $1_x == cp_x ];then
	echo $PWD
	cp $NPWD/env.txt ~/tftp
	cp $NPWD/ubootbuild/u-boot-dtb.bin ~/tftp
	cp $NPWD/ubootbuild/spl/sunxi-spl.bin ~/tftp
	cp $NPWD/linuxbuild/arch/arm/boot/zImage ~/tftp
	cp $NPWD/linuxbuild/arch/arm/boot/dts/sun8i-h3-orangepi-one.dtb ~/tftp
elif [ $1_x == scr_x ];then
	echo $PWD
	mkimage -C none -A arm -T script -d boot.cmd boot.scr
elif [ $1_x == env_x ];then
	echo $PWD
	mkenvimage -s 0x2000 -o uboot.env env.txt
elif [ $1_x == atf_x ];then
	cd $CODETOP/src/arm-trusted-firmware
	echo $PWD  #arm-trusted-firmware
	mkdir -p $NPWD/atfbuild
	make  CROSS_COMPILE=$CROSS_COMPILE BUILD_BASE=$NPWD/atfbuild PLAT=sun50i_a64  $2
	#cp
elif [ $1_x == uboot_x ];then
	cd $CODETOP/src/uboot
	echo $PWD  #am335x_evm
	mkdir -p $NPWD/ubootbuild
	make ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE O=$NPWD/ubootbuild $2
	cp $NPWD/env.txt ~/tftp
	cp $NPWD/ubootbuild/u-boot-dtb.bin ~/tftp
	cp $NPWD/ubootbuild/spl/sunxi-spl.bin ~/tftp
elif [ $1_x == linux_x ];then
	cd $CODETOP/src/linux-5.7.7
	echo $PWD
	if [ $2_x == modules_install_x ];then
		mkdir -p $NPWD/rootfs
		make ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE INSTALL_MOD_PATH=$NPWD/rootfs O=$NPWD/linuxbuild $2
	elif [ $2_x == modules_x ];then
		make ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE INSTALL_MOD_PATH=$NPWD/rootfs O=$NPWD/linuxbuild $2 $3
	else
		mkdir -p $NPWD/linuxbuild
		make ARCH=$ARCH CROSS_COMPILE=$CROSS_COMPILE O=$NPWD/linuxbuild $2
		cp $NPWD/linuxbuild/arch/arm/boot/zImage ~/tftp
		cp $NPWD/linuxbuild/arch/arm/boot/dts/sun8i-h3-orangepi-one.dtb ~/tftp
	fi
elif [ $1_x == busybox_x ];then
	cd $CODETOP/src/busybox
	echo $PWD
	if [ $2_x == install_x ];then
		mkdir -p $NPWD/rootfs
		make O=$NPWD/bboxbuild CONFIG_PREFIX=$NPWD/rootfs $2
	else
		mkdir -p $NPWD/bboxbuild
		make O=$NPWD/bboxbuild $2
	fi
fi
